const Base = require('./base.js');

module.exports = class extends Base {
  //显示预约分享的个人中心页面
  async indexAction(){
    let user_id = this.get('uid');

    this.assign('user_id',user_id);

    return this.display();
  }

  async getinfoAction(){
    let user_id = this.post('user_id');
    let begin_time = this.post('begin_time');
    let end_time = this.post('end_time');

    let info = await this.model().query("select au.name,op.name as jobname,orz.name as zhichengname,au.headurl,od.name as dname from `think_or_app_user` as au left join `think_or_position` as op on au.positionid = op.id left join `think_or_position` as orz on au.zhichengid = orz.id left join `think_or_department` as od on od.id = au.shopid where au.id = " + user_id);

    //获取对应的uid被预约的次数
    let all_count = await this.model().query(' select count(*) as all_num from `think_yuyue` where u_id = ' + user_id);

    //获取当日的预约次数
    let t_count = await this.model().query(' select count(*) as t_num from `think_yuyue` where u_id = ' + user_id + ' and yuyue_time > ' + begin_time + ' and yuyue_time <=' + end_time);

    let data = {
        "info":info,
        "all_count":all_count[0].all_num,
        "t_count":t_count[0].t_num,
    };

    if(think.isEmpty(info)){
      return this.json({'code':"200",'msg':"没有找到数据...",'data':data});
    }else{
      return this.json({'code':"100",'msg':"success...",'data':data});
    }
  }

  //预约分享登录页面
  async loginAction(){
    let user_id = this.get('user_id');
    this.assign('user_id',user_id);
    
    return this.display();
  }

  //客户选择时间的页面
  async choosetimeAction(){
    let user_id = this.get('user_id');
    let sMobile = this.get('sMobile');
    let sName = this.get('sName');

    this.assign({
      "user_id":user_id,
      "sMobile":sMobile,
      "sName":sName,
    });

    return this.display();
  }

  //获取预约的信息
  async getyuyuelistAction(){
    let user_id = this.post('user_id');
    let begin_time = this.post('begin_time');
    let end_time = this.post('end_time');

    let yuyuelist = await this.model().query("select * from `think_yuyue` where u_id = " + user_id + " and yuyue_time > " + begin_time + " and yuyue_time <= " + end_time + " and status = 1");

    return think.isEmpty(yuyuelist) ? this.json({'code':"200",'msg':"数据为空",'data':''}) : this.json({'code':"100",'msg':"查找成功",'data':yuyuelist}) ;
  }

  //提交预约的信息
  async postyuyueAction(){
    let user_id = this.post('user_id');

    let info = await this.model('or_app_user').field('shopid').where({id:user_id}).find();

    let data;
    data = {
      "u_id" : this.post('user_id'),
      "yuyue_time" : this.post('yuyue_time'),
      "add_time" : this.post('addtime'),
      "phone" : this.post('sMobile'),
      "k_name" : this.post('sName'),
      "num" : this.post('mynum'),
      "subject_id" : this.post('mysubject'),
      "remarks" : this.post('myremarks'),
      "shop_id" : info.shopid,
      "status" : 1,
    };

    //先查找数据库里当前时间有没有被预约
    let checkinfo = await this.model().query("select * from `think_yuyue` where yuyue_time = " + this.post('yuyue_time') + " and u_id = " + user_id + " and status = 1");

    let res = [];
    let msg = '预约失败...';

    if(think.isEmpty(checkinfo)) {
      //表示当前美容师的当前预约时间没有被预约
      res = await this.model('yuyue').add(data);
    } else {
      msg = '当前时间已被预约！';
    }

    return think.isEmpty(res)?this.json({'code':"200",'msg': msg,'data':''}):this.json({'code':"100",'msg':"预约成功...",'data':res});
  }

  //选择预约项目的页面
  async yuyuexmAction(){
    let user_id = this.get('user_id');
    let yuyue_time = this.get('yuyue_time');
    let sMobile = this.get('sMobile');
    let sName = this.get('sName');

    this.assign({
      "user_id":user_id,
      "yuyue_time":yuyue_time,
      "sMobile":sMobile,
      "sName":sName,
    });

    return this.display();
  }

  //选择预约项目的页面
  async getyuyueinfoAction(){
    let user_id = this.post('user_id');

    let userinfo = await this.model().query("select au.*,op.name as zhiwei,o.name as zhicheng from `think_or_app_user` as au left join `think_or_position` as op on op.id =au.positionid left join `think_or_position` as o on o.id = au.zhichengid where au.id = " + user_id);

    //获取这家店的项目列表
    // let subjectlist = await this.model().query("select os.id,os.name from `think_or_shop_subject` as sub left join `think_or_subject` as os on sub.departmentid = os.id left join `think_or_app_user` as au on au.shopid = sub.departmentid where au.id = " + user_id);

    let subjectlist = await this.model().query("select s.id,s.name from `think_or_subject` as s left join `think_or_shop_subject` as oss on s.id = oss.subjectid left join `think_or_app_user` as au on au.shopid = oss.departmentid where au.id = " + user_id);

    return this.json({"userinfo":userinfo,"subjectlist":subjectlist});
  }

  //跳转我的预约的页面
  async myyuyueAction(){
    let sMobile = this.get('sMobile');
    let sName = this.get('sName');

    this.assign({
      "sMobile":sMobile,
      "sName":sName,
    });

    return this.display();
  }

  //获取我的预约信息
  async getyulistAction(){
    let sMobile = this.post('sMobile');
    let sName = this.post('sName');

    let yuyuelist = await this.model().query("select y.id,y.yuyue_time,au.name as aname,os.name as sname,od.name as dname,au.headurl from `think_yuyue` as y left join `think_or_app_user` as au on au.id = y.u_id left join `think_or_subject` as os on os.id = y.subject_id left join `think_or_department` as od on od.id = y.shop_id where y.phone = " + sMobile + " and y.status = 1");

    return think.isEmpty(yuyuelist) ? this.json({'code':"200",'msg': '没有数据...','data':''}) : this.json({'code':"100",'msg':"查找成功...",'data':yuyuelist});
  }

  //客户取消预约的信息
  async cancelyuyueAction(){
    let yid = this.post('yid');

    let res = await this.model('yuyue').where({id:yid}).delete();

    return res ? this.json({"code":"100","msg":"删除成功"}) : this.json({"code":"200","msg":"删除失败"}) ;
  }

};
